CONTACTS.Reports : {

	
	/**********************************************************************
	 * Create event handlers and/or perform other initializations
	 *********************************************************************/
	initialize : function() {
	
		CONTACTS.Reports.init_create_report();
		
		//update report on change to filter checkboxes
		$('#form_filters input[type=checkbox]').live('click',function(){
			
			//hide notification box
			COMMON.hide_notification('#form_filters .box');
			
			//show "loading" animation
			$('#form_filters .loader').show();
			
			//define error handler
			var error_occurred = function() {
				COMMON.show_notification('#form_filters .box','box-error','Error occurred. Please try again.',null);
				$('#form_filters .loader').hide();
			};
			
			//remove all fields
			$('#report table tr').remove();

			//prepare the data to be sent
			form_data = { 'options' : [] };
			$('#form_filters input[type=checkbox]:checked').each(function(){
				var data_type = $(this).attr('data-type');
				form_data['options'].push( { 'data_type' : $(this).attr('data-type'), 'value' : $(this).val() } );
			});
			
			//send the data
			$.ajax({
				type: 'POST',
				url: COMMON.base_url + 'contacts/ajax_create_report',
				data: { 'json':JSON.stringify(form_data) },
				dataType: 'json',
				success: function(return_data) {
					
					if (return_data) {
						
						
						
						$('#form_filters .loader').hide();
						
						
					} else {
						error_occurred();
					}
				},
				error: error_occurred
			});
		});
		$('#form_filters input[type=text]').keyup(function(){ $('#report .title').html($(this).val()); });
		
	
	}, //end CONTACTS.Reports.initialize()
	
	
	/**********************************************************************
	 * Initialize "Create Report" box
	 *********************************************************************/
	init_create_report : function() {
	
		//event handlers
		$('#form_filters .contact_type').live('click',function() { CONTACTS.Reports.update_contact_fields(); });
	
		//hide notification box
		COMMON.hide_notification('#form_filters .box');

		//show "loading" animation
		$('#form_filters .loader').show();

		//define error handler
		var error_occurred = function() {
			COMMON.show_notification('#form_filters .box','box-error','Error occurred: could not retrieve contact types.',null);
			$('#form_filters .loader').hide();
		};
		
		//populate contact types and companies
		$.ajax({
			type: 'POST',
			url: COMMON.base_url + 'contacts/ajax_get_new_contact_fields',
			data: { },
			dataType: 'json',
			success: function(return_data) {
				if (return_data) {
					for (i in return_data['contact_types']) {
						$('#form_filters .contact_types').html($('#form_filters .contact_types').html() + '<label class="choice"><input type="checkbox" value="' + i + '" class="contact_type" data-type="contact_type" /> ' + return_data['contact_types'][i] + '<br /></label>');
					}
					$('#form_filters .loader').hide();
					$('#form_filters .contact_types').show('slow');
				} else {
					error_occurred();
				}
			},
			error: error_occurred
		}); 
		
	}, //end CONTACTS.Reports.init_create_report
	
	
	/**********************************************************************
	 * Add or remove contact fields when contact types are changed
	 *********************************************************************/
	update_contact_fields : function() {
		
		//hide notification box
		COMMON.hide_notification('#form_filters .box');
		
		//show "loading" animation
		$('#form_filters .loader').show();
		
		//define error handler
		var error_occurred = function() {
			COMMON.show_notification('#form_filters .box','box-error','Error occurred: could not retrieve contact fields.',null);
			$('#form_filters .loader').hide();
		};
		
		//remove all fields
		$('#form_filters table .contact_fields').hide().html('');

		//prepare the data to be sent
		form_data = {
			contact_type_ids : [],
			contact_company_id : null
		};
		$('#form_filters .contact_type:checked').each(function(){
			form_data['contact_type_ids'].push($(this).val());
		});
		
		//send the data
		$.ajax({
			type: 'POST',
			url: COMMON.base_url + 'contacts/ajax_get_contact_fields',
			data: { 'json':JSON.stringify(form_data) },
			dataType: 'json',
			success: function(return_data) {
				
				if (return_data) {
					
					//display fields
					for (i in return_data['contact_fields']) {						
						$('#form_filters table .contact_fields').append('\
							<label class="choice"><input type="checkbox" value="' + return_data['contact_fields'][i].contact_field_id + '" data-type="contact_field" data-name="' + return_data['contact_fields'][i].name + '" /> ' + return_data['contact_fields'][i].name + '</label><br />\
						');
					}
					$('#form_filters .loader').hide();
					$('#form_filters table .contact_fields').show('slow');
					
				} else {
					error_occurred();
				}
			},
			error: error_occurred
		});
	}, //end CONTACTS.Reports.update_contact_fields()
	
};